<?php


namespace apps\marriage\app\op\controller;


use apps\marriage\core\model\Activity;
use apps\marriage\core\service\ActivityService;
use think\Request;

class ActivityController extends BaseMarriageOpController
{
    /**
     * @var ActivityService
     */
    private $activityService;

    protected function init()
    {
        parent::init();
        $this->activityService = service('Activity', SERVICE_NAMESPACE);
    }

    public function add(Request $request)
    {
        $data = $request->param();
        $data['status'] = Activity::STATUS_ONLINE;
        $data['add_time'] = time();
        $this->activityService->create($data);
        return $this->success();
    }

    public function edit(Request $request)
    {
        $data = $request->param();
        $this->activityService->updateByPk($data);
        return $this->success();
    }

    public function info(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $activity = $this->activityService->getByPk($id);
        $activity = $this->activityService->format($activity);
        return $this->success([
            'activity' => $activity
        ]);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'status' => Activity::STATUS_ONLINE
        ];
        $res = $this->activityService->pageListByParams($params, $pageSize);
        if($res['data']) {
            foreach ($res['data'] as &$rs) {
                $rs = $this->activityService->format($rs);
            }
        }
        return $this->success($res);
    }

    public function offline(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $this->activityService->updateByPk([
            'id' => $id,
            'status' => Activity::STATUS_OFFLINE
        ]);
        return $this->success();
    }

    public function sign_in_qrcode(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $img = $this->activityService->getSignInQrCodeImg($id);
        return $this->success(['img' => $img]);
    }
}